﻿#include "formmulticolumn.h"
#include "qtpropertymanager.h"
#include "qtvariantproperty.h"
#include "ui_formmulticolumn.h"
#include <QDebug>

FormMultiColumn::FormMultiColumn(QWidget *parent)
    : QWidget(parent),
      ui(new Ui::FormMultiColumn)
{
    ui->setupUi(this);
    demo();
}

FormMultiColumn::~FormMultiColumn()
{
    delete ui;
}

void FormMultiColumn::demo()
{
    //自定义标题
    QStringList head;
    head << tr("科目") << tr("张三") << tr("李四");
    ui->widget->setHeaderLabels(head);

    //右键菜单
    QAction *actionCopy = new QAction(tr("复制(C)   Ctrl+C"), ui->widget);
    connect(actionCopy, &QAction::triggered, this, &FormMultiColumn::slotCopy);
    ui->widget->setContextMenu(actionCopy);

    //属性表
    QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget);

    QtVariantProperty *item = pVarManager->addProperty(QVariant::Int, tr("整形数据"));
    item->setValue(235);
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::Bool, tr("布尔型数据"));
    item->setValue(true);
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::Double, tr("浮点数据"));
    item->setAttribute(QLatin1String("decimals"), 5);
    item->setValue(3.1415926);
    ui->widget->addProperty(item);

    item = pVarManager->addProperty(QVariant::String, tr("字符串数据"));
    item->setValue(tr("你好!世界!"));
    QtBrowserItem *p = ui->widget->addProperty(item);

    //背景色
    ui->widget->setBackgroundColor(p, QColor(qRgb(255, 255, 0)));
}

void FormMultiColumn::variantPropertyValueChanged(QtProperty *property, const QVariant &value)
{
}

void FormMultiColumn::slotCopy()
{
#if 1 //复制鼠标所在单元格的数据
    QModelIndex index = ui->widget->currentIndex();
    QString str = index.data().toString();
    QApplication::clipboard()->setText(str);
#else //复制鼠标所在行的数据
    QModelIndexList indexes = ui->widget->selectedIndexes();
    if (indexes.count() == 0)
    {
        //select nothing
        return;
    }

    QStringList list;
    foreach (const QModelIndex &index, indexes)
    {
        list << index.data().toString();
    }

    QApplication::clipboard()->setText(list.join(","));
#endif
}
